package com.graduation.rescuematerial.controller;

import com.graduation.rescuematerial.service.PicService;
import com.graduation.rescuematerial.vo.RespBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @Author:CaiShuangLian
 * @FileName:
 * @Date:Created in  2022/4/16 16:56
 * @Version:
 * @Description:TODO
 */

@RestController
@RequestMapping("pic")
@Api(tags = "图片模块")
@Slf4j
public class PicController {

    @Autowired
    private PicService picService;

    @PostMapping("/uploadImg")
    @ResponseBody
    @ApiOperation("上传图片并返回URL链接")
    public RespBean uploadImg(@ApiParam("file：文件名") @RequestParam("file") MultipartFile image, HttpServletRequest request){
        RespBean success=new RespBean();

        if(image.isEmpty()){
            success.setStatus(400);
            success.setMsg("文件为空，请重新上传");
            return success;
        }
        try {
            String fileUrl= picService.uploadImg(image);
            success.setStatus(200);
            success.setMsg("文件上传成功");
            success.setData(fileUrl);

            return success;
        }catch (IOException e){
            e.printStackTrace();
        }
        success.setData(5000000);
        success.setMsg("文件上传发生异常");
        return success;

    }

    //时间充裕增加图片审核功能
}
